package com.lidong.consumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;


import java.util.HashMap;
import java.util.Map;

@RestController
public class WebController {

    private static final String SERVICE_NAME = "SPRING-CLOUD-EUREKA-PRODUCER";
    @Autowired
	private RestTemplate restTemplate;

	@HystrixCommand(fallbackMethod = "testError",commandProperties = {
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "4000")

	})
	@GetMapping("/test")
    public String test(@RequestParam(value = "name")String name) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("name",name);
        String result = restTemplate.getForObject("http://"+SERVICE_NAME+"/hello?name={name}", String.class,paramMap);
        System.out.println(result);
        return result;
    }

	public String testError(String dd) {
		return "出错之后会返回这里";
	}

}
